package com.db;

import com.jfinal.config.Plugins;
import com.jfinal.kit.PathKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.DbKit;
import com.jfinal.plugin.druid.DruidPlugin;
import com.vvss.jfinal.Elephant;
import com.vvvsss.jfinal3.model._MappingKit;
import lombok.experimental.UtilityClass;
import lombok.extern.log4j.Log4j;
import org.beetl.sql.core.SQLManager;
import org.beetl.sql.core.mapper.builder.MapperConfigBuilder;
import org.beetl.sql.core.mapper.internal.AllCountAmi;
import org.beetl.sql.core.mapper.internal.SingleAmi;
import org.beetl.sql.experimental.iohao.core.$Mapper;
import org.beetl.sql.experimental.iohao.core.MyMapper;
import org.beetl.sql.experimental.iohao.core.SqlKit;
import org.beetl.sql.experimental.iohao.mapper.methoddesc.MethodDescExperimental;

import static com.jfinal.plugin.activerecord.DbKit.MAIN_CONFIG_NAME;

/**
 * <BR>
 * create time: 2017/4/17 18:00
 *
 * @author luoyizhu
 */
@UtilityClass
@Log4j
public class Config {
    public String userName = "root";
    public String password = "";
    public String url = "jdbc:mysql://localhost:3306/aabbcc?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull";
    public String driver = "com.mysql.jdbc.Driver";
    boolean init;
    boolean Jinit;

    boolean Jinit3;

    boolean kotlinInit;

    public void privateConfig() {
        /*
         * 这个类是不提交的,里面就是改变了Config的数据库连接信息.
         * 如果你是拉取代码运行示例的,请删除这行代码
         */
        PrivateConfig.settingConfig();
    }

    public void dbInitKotlin() {
        if (kotlinInit) {
            return;
        }

        kotlinInit = true;

        privateConfig();

        // 构建SQLManager, 并添加上监控sql执行情况的sqlDebug的拦截器.
        SQLManager sqlManager = SQLManager.newBuilder(driver, url, userName, password).addInterDebug().build();
        SqlKit.$(sqlManager);

        /*
         * 使用自定义基接口, 不使用beetlsql提供的接口.
         * 这样可以使项目中的代码与框架无任何粘性.
         * 这样做的好处是将来有比beetlsql更优秀的orm, 可以随时替换, 而且不需要改动任何业务代码.
         */
        MapperConfigBuilder builder = sqlManager.setBaseMapper(MyMapper.class).getBuilder();
        builder.setMethodDescBuilder(MethodDescExperimental::new);

        // 给基接口扩展一个方法.
        builder.addAmi("getById", new SingleAmi());

        builder.addAmi("count", new AllCountAmi());
    }

    /**
     * beetlsql 配置
     */
    public void dbInit() {
        if (init) {
            return;
        }

        init = true;

        privateConfig();

        // 构建SQLManager, 并添加上监控sql执行情况的sqlDebug的拦截器.
        SQLManager sqlManager = SQLManager.newBuilder(driver, url, userName, password).addInterDebug().build();

        SqlKit.$(sqlManager);

        /*
         * 使用自定义基接口, 不使用beetlsql提供的接口.
         * 这样可以使项目中的代码与框架无任何粘性.
         * 这样做的好处是将来有比beetlsql更优秀的orm, 可以随时替换, 而且不需要改动任何业务代码.
         */
        sqlManager.setBaseMapper($Mapper.class);
    }

    /**
     * jfianl 2.x 的总结示例配置
     */
    public void jfinalInit() {
        if (Jinit) {
            return;
        }

        Jinit = true;

        privateConfig();

        Plugins me = new Plugins();

        DruidPlugin druidPlugin = new DruidPlugin(url, userName, password, driver);
        me.add(druidPlugin);

        ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
        arp.addMapping("tb_bird", Elephant.class);
        me.add(arp);

        me.getPluginList().forEach(o -> o.start());
    }

    /**
     * jfinal 3.1 的总结示例配置
     */
    public void jfinalInit3() {
        if (Jinit3) {
            return;
        }

        Jinit3 = true;

        privateConfig();


        if (DbKit.getConfig(MAIN_CONFIG_NAME) != null) {
            DbKit.removeConfig(MAIN_CONFIG_NAME);
        }

        Plugins me = new Plugins();

        DruidPlugin druidPlugin = new DruidPlugin(url, userName, password, driver);
        me.add(druidPlugin);

        ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
        me.add(arp);

        arp.setShowSql(true);

        arp.setBaseSqlTemplatePath(PathKit.getWebRootPath() + "/resources/test/jfinal/mysql");

        // 需要手动管理sql文件.(如果有百个sql文件,需要逐个添加)
        arp.addSqlTemplate("animal.sql");

        _MappingKit.mapping(arp);


        me.getPluginList().forEach(o -> o.start());
    }

}
